package atguigu.web;

import atguigu.pojo.Book;
import atguigu.pojo.Cart;
import atguigu.pojo.CartItem;
import atguigu.service.BookService;
import atguigu.service.impl.BookServiceImpl;
import atguigu.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class CartServlet extends BaseServlet {
    BookService  bookService = new BookServiceImpl();
    protected void ajaxAddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数 商品编号
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        //2.调用bookService.queryBookById(id) 得到图书信息
        Book book = bookService.queryBookById(id);
        //3.把图书信息 转换成为DartItem商品项
        CartItem cartItem = new CartItem(book.getId(),book.getName(),1,new BigDecimal(book.getPrice()),new BigDecimal(book.getPrice()));
        //4.调用Cart.addItem(CarItem）添加商品项
        Cart cart =(Cart) req.getSession().getAttribute("cart");
        if(cart == null){
            cart = new Cart();
            req.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);
        //6.保存最后一个添加商品的名称
        req.getSession().setAttribute("lastName",cartItem.getName());
        //7.用ajax 返回购物车总的商品数量和最后一个添加的商品名称
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("totalCount",cart.getTotalCount());
        resultMap.put("lastName",cartItem.getName());
        Gson gson = new Gson();
        String resultMapJsonString = gson.toJson(resultMap);
        resp.getWriter().write(resultMapJsonString);
    }
    //添加到购物车
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数 商品编号
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        //2.调用bookService.queryBookById(id) 得到图书信息
        Book book = bookService.queryBookById(id);
        //3.把图书信息 转换成为DartItem商品项
        CartItem cartItem = new CartItem(book.getId(),book.getName(),1,new BigDecimal(book.getPrice()),new BigDecimal(book.getPrice()));
        //4.调用Cart.addItem(CarItem）添加商品项
        Cart cart =(Cart) req.getSession().getAttribute("cart");
        if(cart == null){
             cart = new Cart();
             req.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);
        //5.重定向回到原来商品所在的地址页面
        resp.sendRedirect(req.getHeader("Referer"));
        //6.保存最后一个添加商品的名称
        req.getSession().setAttribute("lastName",cartItem.getName());
    }
    //删除商品
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数 商品编号
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        //2.获取购物车对象
        Cart cart =(Cart) req.getSession().getAttribute("cart");
        if(cart != null){
            //删除商品
           cart.deleteItem(id);
            //重定向回到原来商品所在的地址页面
            resp.sendRedirect(req.getHeader("Referer"));
        }

    }
    //清空购物车
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取购物车对象
        Cart cart =(Cart) req.getSession().getAttribute("cart");
        if(cart !=null){
            //清空购物车
           cart.clear();
            //重定向回到原来商品所在的地址页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }
    //修改商品数量
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数 商品编号 这个id,count是cart.jsp页面发送过来的
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        int count = WebUtils.parseInt(req.getParameter("count"),1);
        //获取购物车对象
        Cart cart =(Cart) req.getSession().getAttribute("cart");
        if(cart !=null){
            //修改商品数量
            cart.updateCount(id,count);
            //重定向回到原来商品所在的地址页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }
}
